﻿package {
	import flash.display.*;
	import flash.events.*;
	import flash.utils.Timer;
	import flash.text.*;
	
	import lt.uza.utils.*;
	
	import ProximityManager;

	import Camera2D;
	
	import Titan_Icon;
	import Titan;
	import Aura;
	import God;
	
	import Gene;
	import Genebank;

	
	//import visible_Aura;
	
	public class Titans extends MovieClip {
		
		//VARS
		private var global:Global = Global.getInstance();
		
		var pm:ProximityManager = new ProximityManager(48);
		
		 var dashboard:TextField = new TextField();
		 
		 var GODSPANS:int=new int(0);
		 var MAXSTR:int=new int(100);
		
		var GameTimer:Timer = new Timer(1000/30); //30 fps
		var ent_Titans:Array =new Array();
		var ent_Auras:Array =new Array();
		var ent_Gods:Array =new Array();
		var ent_Titan_Icons:Array=new Array();
		
		var aura_shapes:Shape = new Shape();
		var debug_shapes:Shape = new Shape();
		
		var visible_auras:Array = new Array();
		
		var cameraMove:Boolean=false;
		
		var oldmouseX:Number;
		var oldmouseY:Number;
		
		var mouseMode:int=0;//	0=none
							//	1=selection
							//	2=move;
		
		public function Titans():void{
			//INIT
			
			global.chromosomes=new Genebank();
			
			global.chromosomes.fill(2);
			//trace(20%2);
			global.chromosomes.printGenes();
			
			
			global.camera=new Camera2D;
			
			global.stageHeight=stage.stageHeight;
			global.stageWidth=stage.stageWidth;
			global.auratimer=0;
			addChild(aura_shapes);
			aura_shapes.alpha=0.6;
			
			addChild(debug_shapes);
			addChild(dashboard);
			GameTimer.addEventListener(TimerEvent.TIMER, mainGameLoop); //add global gametimer
			
			stage.addEventListener(MouseEvent.MOUSE_DOWN, MouseDown);
			stage.addEventListener(MouseEvent.MOUSE_UP, MouseUp);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, MouseMove);
			
			//INIT TITANS
			var NUM_TITANS:int=6;
			
			for(var i:int=0; i<NUM_TITANS;i++){
				var t_titan = new Titan();
				ent_Titans.push(t_titan);
				ent_Titans[i].ID=i;
				ent_Titans[i].setChromosomes();
				this.addChild(ent_Titans[i]);
				
				var t_titan_icon = new Titan_Icon();
				ent_Titan_Icons.push(t_titan_icon);
				ent_Titan_Icons[i].ID=i;
				ent_Titan_Icons[i].r=ent_Titans[i].r;
				ent_Titan_Icons[i].g=ent_Titans[i].g;
				ent_Titan_Icons[i].B=ent_Titans[i].B;
				ent_Titan_Icons[i].Update_Color();
				this.addChild(ent_Titan_Icons[i]);
				ent_Titan_Icons[i].visible=false;
				
			}			
			
			
			/*var t_God:God= new God();
			t_God.x=100;
			t_God.y=100;
			ent_Gods.push(t_God);
			this.addChild(ent_Gods[ent_Gods.length-1]);*/
			
			GameTimer.start();
		}
		
		function mainGameLoop(event:TimerEvent):void{
	
			//TITAN BEHAVIOUR LOOP
			for(var i:int=0;i<ent_Titans.length;i++)
			{

					var o: int = 10;
						//CHECK OF TITAN IN AURA STAAT
						for(var j:int=0;j<ent_Auras.length;j++){
							if(ent_Auras[j].IDparent == ent_Titans[i].ID){
								var tx = Math.abs(ent_Auras[j].x - ent_Titans[i].x);
								var ty = Math.abs(ent_Auras[j].y - ent_Titans[i].y);
								var ta = Math.sqrt(tx*tx+ty*ty);
								
								if(ent_Auras[j].str/2 < ta){
									ent_Auras[j].str-=Math.log(1.09);
								}
								if(ent_Auras[j].str/2 > ta ){
									if(ent_Auras[j].str<MAXSTR){
										ent_Auras[j].str+=Math.log(1.07);}
									o = 15;
								}
							}
						}
						if(global.auratimer==90){
							if(o==10){ //ZO NIET MAAK NIEUWE AURA
								var t_Aura = new Aura(ent_Auras.length, 5, i, ent_Titans[i].x, ent_Titans[i].y);
								ent_Auras.push(t_Aura);
								global.auratimer=0;
								pm.addItem(ent_Auras[ent_Auras.length-1]);
							}
						}else{
							global.auratimer++;
						}
				
				ent_Titans[i].Update();	
				
				
				//////TITAN ICONS BEHAVIOUR /////////
				ent_Titan_Icons[i].visible=false;
				
				if(ent_Titans[i].Visible==false){
					
					ent_Titan_Icons[i].y=ent_Titans[i].y;
					ent_Titan_Icons[i].x=ent_Titans[i].x;
					
					if(ent_Titans[i].x>global.stageWidth){
						ent_Titans[i].Visible=false;
						ent_Titan_Icons[i].x=global.stageWidth-10;
						
					}
					if(ent_Titans[i].x<0){
						ent_Titans[i].Visible=false;
						ent_Titan_Icons[i].x=10;
					}
					if(ent_Titans[i].y>global.stageHeight){
						ent_Titans[i].Visible=false;
						ent_Titan_Icons[i].y=global.stageHeight-10;
						
					}
					if(ent_Titans[i].y<0){
						ent_Titans[i].Visible=false;
						ent_Titan_Icons[i].y=10;
						
					}
					if(!ent_Titans[i].Visible){
						ent_Titans[i].placeholder.visible=false;
						ent_Titan_Icons[i].visible=true;
					}else{
						ent_Titan_Icons[i].visible=false;
					}
				}
				
				
			}
			//GOD BEHAVIOUR LOOP
			for(var p:int=0;p<ent_Gods.length;p++)
			{
				ent_Gods[p].Update();
			}
			//////AURA BEHAVIOUR LOOP//////
			
			debug_shapes.graphics.clear();
			
			pm.refresh();
			GODSPANS=0;
			for(var a:int=0;a<ent_Auras.length;a++)
			{
				ent_Auras[a].Update();
				
				///////VERWIJDER OUDE AURAS///////
				if(ent_Auras[a].str<1){ 
					pm.removeItem(ent_Auras[a]);
					ent_Auras.splice(a,1);
					
				}
							
				
				
				////////AURA COLLISION (GODSPAWN ETC) ////////
				debug_shapes.graphics.lineStyle(2, 0xFFD700);
				
				var neighbors:Array = pm.getNeighbors(ent_Auras[a]);
				
				
				var l:uint = neighbors.length;
				debug_shapes.graphics.moveTo(ent_Auras[a].x,ent_Auras[a].y);	
				
				for (var m:uint=1; m<l; m++) {	
					if(!(neighbors[m].IDparent==ent_Auras[a].IDparent)){
						
						var aa=ent_Auras[a].x-neighbors[m].x;
						var b=ent_Auras[a].y-neighbors[m].y;
						var c=Math.sqrt(aa*aa+b*b);
						
						if(c+c<ent_Auras[a].str+neighbors[m].str){
							debug_shapes.graphics.lineTo(neighbors[m].x,neighbors[m].y);
							//GODSPANS++;
							
							if(ent_Auras[a].str>25&&+neighbors[m].str>25){	//SPAWN GOD
								//trace(ent_Auras[a].childList.indexOf(neighbors[m].childList[0]));
								if((ent_Auras[a].childList.indexOf(neighbors[m].childList[0])==-1)){
										
									//trace("spawn!:" +neighbors[m].childList[0]);
									//trace(ent_Auras[a].childList);
									ent_Auras[a].childList.push(neighbors[m].childList[0]);
									neighbors[m].childList.push(ent_Auras[a].childList[0]);
									var t_God:God= new God();
									
									if(ent_Auras[a].x<neighbors[m].x){			
										t_God.x=ent_Auras[a].x+(neighbors[m].x-ent_Auras[a].x)/2;
									}else{
										t_God.x=neighbors[m].x+(ent_Auras[a].x-neighbors[m].x)/2;
									}
									if(ent_Auras[a].y<neighbors[m].y){			
										t_God.y=ent_Auras[a].y+(neighbors[m].y-ent_Auras[a].y)/2;
									}else{
										t_God.y=neighbors[m].y+(ent_Auras[a].y-neighbors[m].y)/2;
									}
									
									t_God.placeholder_chromosome.push(ent_Titans[ent_Auras[a].IDparent].placeholder_chromosome[int(Math.random()*1)]);
									t_God.placeholder_chromosome.push(ent_Titans[neighbors[m].IDparent].placeholder_chromosome[int(Math.random()*1)]);
									trace("GOD"+ent_Gods.length+1+": "+ ent_Titans[ent_Auras[a].IDparent].placeholder_chromosome[0].name+", "+ent_Titans[ent_Auras[a].IDparent].placeholder_chromosome[1].name);
									ent_Gods.push(t_God);
									
									
																																		 
																																		 
									this.addChild(ent_Gods[ent_Gods.length-1]);
								}
								
							}
						}
					}
					
					
				}
				
				
				
				
			}
			UpdateAuraShapes(); 	//render auras
			
			global.camera.Reset();
			
			
			dashboard.text=
			 " GODS: "+ent_Gods.length
			+"\r AURAS: "+ent_Auras.length;
			
			
			
			this.setChildIndex(debug_shapes,this.numChildren-1);
		}
		
		 
		 function MouseDown(evt:MouseEvent):void{	//MUISKLICK DESTINATION TITANS

			if(mouseMode==1){//mouse mode is TITAN MOVE
				for(var o:int=0;o<ent_Titans.length;o++)
				{
					if(ent_Titans[o].Selected)
					{
						ent_Titans[o].Goto(mouseX,mouseY);
						ent_Titans[o].Idle=false;
					}	
				}
				mouseMode =0;
			}			
			
			if(!mouseMode==1){//mouse mode is CAMERA MOVE 
				mouseMode=2;	
				oldmouseX=mouseX;
	 			oldmouseY=mouseY;
			}

			for(var i:int=0;i<ent_Titans.length;i++) //Check for TITAN SELECT
			{
				ent_Titans[i].Selected=false;
				if(mouseX > (ent_Titans[i].x-10) && mouseX < (ent_Titans[i].x+10) && mouseY > (ent_Titans[i].y-10) && mouseY < (ent_Titans[i].y+10))
					{
					mouseMode=1; 
					ent_Titans[i].Selected=true;
					i=ent_Titans.length;
					}
				//ent_Titans[i].Update();		
			}
		
			
			for(var j:int=0;j<ent_Titan_Icons.length;j++) //Check for TITAN_ICON SELECT
			{
				if(mouseX > (ent_Titan_Icons[j].x-10) && mouseX < (ent_Titan_Icons[j].x+10) && mouseY > (ent_Titan_Icons[j].y-10) && mouseY < (ent_Titan_Icons[j].y+10))
				{
					//trace(ent_Titans[j].x+" "+ent_Titans[j].y);
					var cammovex:Number = ((global.stageWidth*0.5)-ent_Titans[j].x);
					var cammovey:Number = ((global.stageHeight*0.5)-ent_Titans[j].y);
					//trace (cammovex + " " + cammovey);
					global.camera.move(cammovex,cammovey);
					j=ent_Titan_Icons.length;
				}
			}

		}
		
		 function MouseUp(evt:MouseEvent):void{
			if(mouseMode==2){
				mouseMode=0; 
			}
	 
			
			 
		 }
		 function MouseMove(evt:MouseEvent):void{y

			 if(mouseMode==2){	//move the camera if mouse is in move mode
				// trace("_viewx: "+global.camera._viewx + ", _viewy: " +global.camera._viewy);

				 global.camera.move(-(oldmouseX-mouseX)/15,-(oldmouseY-mouseY)/15);
		 			
			 }
			 
			 
		 }
		
		function UpdateAuraShapes():void{
			/////////////RENDER AURAS//////////
			aura_shapes.graphics.clear();
					
			for(var i:int=0;i<ent_Auras.length;i++){
			
				//CHECK VISIBILITY	
				if(ent_Auras[i].y-ent_Auras[i].str>global.stageHeight||ent_Auras[i].y+ent_Auras[i].str<0||ent_Auras[i].x+ent_Auras[i].str<0||ent_Auras[i].x-ent_Auras[i].str>global.stageWidth){
					ent_Auras[i].visible=false;
					
				}else{//DRAW VISIBLE AURAS
					ent_Auras[i].visible=true;
					
					aura_shapes.graphics.beginFill(ent_Titans[ent_Auras[i].IDparent].r<<16|ent_Titans[ent_Auras[i].IDparent].g<<8|ent_Titans[ent_Auras[i].IDparent].B);
					aura_shapes.graphics.drawCircle(ent_Auras[i].x, ent_Auras[i].y, ent_Auras[i].str);
					
				}

			}
			
		}
		
	}
}